package net.jarang.android.saleoffline.activity.sale;

import java.util.List;

import net.jarang.android.saleoffline.domain.Sale;
import net.jarang.android.saleoffline.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SaleListAdapter extends ArrayAdapter<Sale> {
	private Context mContext;
	private int mResource;
	private List<Sale> sales;
	private LayoutInflater mInflater;

	public SaleListAdapter(Context context, int layoutResource, List<Sale> sales) {
		super(context, layoutResource, sales);
		this.mContext = context;
		this.mResource = layoutResource;
		this.sales = sales;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Sale sale = sales.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(mResource, parent, false);
		}

		if (sale != null) {
			WebView thumbnail = (WebView)convertView.findViewById(R.id.saleThumbnail);
			thumbnail.setFocusable(false);
			TextView saleNameTV = (TextView) convertView.findViewById(R.id.list_view_saleInfo);
			TextView shopNameTV = (TextView) convertView.findViewById(R.id.list_view_shopName);
			TextView saleDateTV = (TextView) convertView.findViewById(R.id.list_view_saleDate);

//			DisplayMetrics outMetrics = new DisplayMetrics();
//			getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
//			float dpi = outMetrics.densityDpi;
//			
//			float pixcel = dpi * outMetrics.density;
//			Log.v(SaleContext.TAG, "pixcel : " + pixcel);
			
			
			if (thumbnail != null) {
				thumbnail.loadUrl(sale.getThumbnailImageUrl1());
			}
			saleNameTV.setText(sale.displaySaleInfo());
			shopNameTV.setText(sale.getShopName());
			saleDateTV.setText(sale.displaySaleDate());
		}
		return convertView;
	}
	
	public Sale getItem(int position) {
		return sales.get(position);
	}
	
	public long getItemId(int position) {
		return sales.get(position).getId();
	}
}
